package com.lhy.wcss.common.utls;

import com.lhy.wcss.common.BaseConstant;
import com.lhy.wcss.model.domain.Admin;
import com.lhy.wcss.model.domain.WhitelistSetting;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;

public class SessionUtils {
    private static final String USERKEY = "sessionUser";

    public static HttpSession session() {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        return attr.getRequest().getSession(true); // true == allow create
    }

    public static Admin getCurrentUserInfo() {
        return (Admin) session().getAttribute(USERKEY);
    }

    public static void saveCurrentUserInfo(Admin admin) {
        session().setAttribute(USERKEY, admin);
    }

    public static void saveCurUser(WhitelistSetting user) {
        session().setAttribute(BaseConstant.LOGIN_USER_KEY,user);
    }

    public static WhitelistSetting getCurUser() {
        WhitelistSetting attribute =(WhitelistSetting) session().getAttribute(BaseConstant.LOGIN_USER_KEY);
        return attribute;
    }

    public static void removeCurUser() {
        session().removeAttribute(BaseConstant.LOGIN_USER_KEY);
    }
}
